﻿namespace ihongma.Web.Plugin.Feedback.DAL
{
    using ihongma.Common;
    using ihongma.DBUtility;
    using ihongma.Web.Plugin.Feedback.Model;
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Runtime.InteropServices;
    using System.Text;

    public class dfeedback
    {
        public int Add(mfeedback model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into dt_feedback(");
            builder.Append("title,content,user_name,user_tel,user_qq,user_email,add_time,is_lock)");
            builder.Append(" values (");
            builder.Append("@title,@content,@user_name,@user_tel,@user_qq,@user_email,@add_time,@is_lock)");
            OleDbParameter[] parameterArray = new OleDbParameter[] { new OleDbParameter("@title", OleDbType.VarChar, 100), new OleDbParameter("@content", OleDbType.VarChar), new OleDbParameter("@user_name", OleDbType.VarChar, 50), new OleDbParameter("@user_tel", OleDbType.VarChar, 30), new OleDbParameter("@user_qq", OleDbType.VarChar, 30), new OleDbParameter("@user_email", OleDbType.VarChar, 100), new OleDbParameter("@add_time", OleDbType.Date), new OleDbParameter("@is_lock", OleDbType.Integer, 4) };
            parameterArray[0].Value = model.title;
            parameterArray[1].Value = model.content;
            parameterArray[2].Value = model.user_name;
            parameterArray[3].Value = model.user_tel;
            parameterArray[4].Value = model.user_qq;
            parameterArray[5].Value = model.user_email;
            parameterArray[6].Value = model.add_time;
            parameterArray[7].Value = model.is_lock;
            return DbHelperOleDb.ExecuteSql(builder.ToString(), parameterArray);
        }

        public bool Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from dt_feedback ");
            builder.Append(" where id=@id");
            OleDbParameter[] parameterArray = new OleDbParameter[] { new OleDbParameter("@id", OleDbType.Integer, 4) };
            parameterArray[0].Value = id;
            return (DbHelperOleDb.ExecuteSql(builder.ToString(), parameterArray) > 0);
        }

        public bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from dt_feedback");
            builder.Append(" where id=@id");
            OleDbParameter[] parameterArray = new OleDbParameter[] { new OleDbParameter("@id", OleDbType.Integer, 4) };
            parameterArray[0].Value = id;
            return DbHelperOleDb.Exists(builder.ToString(), parameterArray);
        }

        public DataSet GetList(int Top, string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" id,title,content,user_name,user_tel,user_qq,user_email,add_time,reply_content,reply_time,is_lock ");
            builder.Append(" FROM dt_feedback ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by add_time desc");
            return DbHelperOleDb.Query(builder.ToString());
        }

        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * FROM dt_feedback");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperOleDb.GetSingle(PagingHelper.CreateCountingSql(builder.ToString())));
            return DbHelperOleDb.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, builder.ToString(), filedOrder));
        }

        public mfeedback GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 id,title,content,user_name,user_tel,user_qq,user_email,add_time,reply_content,reply_time,is_lock from dt_feedback ");
            builder.Append(" where id=@id");
            OleDbParameter[] parameterArray = new OleDbParameter[] { new OleDbParameter("@id", OleDbType.Integer, 4) };
            parameterArray[0].Value = id;
            mfeedback feedback = new mfeedback();
            DataSet set = DbHelperOleDb.Query(builder.ToString(), parameterArray);
            if (set.Tables[0].Rows.Count > 0)
            {
                if ((set.Tables[0].Rows[0]["id"] != null) && (set.Tables[0].Rows[0]["id"].ToString() != ""))
                {
                    feedback.id = int.Parse(set.Tables[0].Rows[0]["id"].ToString());
                }
                if ((set.Tables[0].Rows[0]["title"] != null) && (set.Tables[0].Rows[0]["title"].ToString() != ""))
                {
                    feedback.title = set.Tables[0].Rows[0]["title"].ToString();
                }
                if ((set.Tables[0].Rows[0]["content"] != null) && (set.Tables[0].Rows[0]["content"].ToString() != ""))
                {
                    feedback.content = set.Tables[0].Rows[0]["content"].ToString();
                }
                if ((set.Tables[0].Rows[0]["user_name"] != null) && (set.Tables[0].Rows[0]["user_name"].ToString() != ""))
                {
                    feedback.user_name = set.Tables[0].Rows[0]["user_name"].ToString();
                }
                if ((set.Tables[0].Rows[0]["user_tel"] != null) && (set.Tables[0].Rows[0]["user_tel"].ToString() != ""))
                {
                    feedback.user_tel = set.Tables[0].Rows[0]["user_tel"].ToString();
                }
                if ((set.Tables[0].Rows[0]["user_qq"] != null) && (set.Tables[0].Rows[0]["user_qq"].ToString() != ""))
                {
                    feedback.user_qq = set.Tables[0].Rows[0]["user_qq"].ToString();
                }
                if ((set.Tables[0].Rows[0]["user_email"] != null) && (set.Tables[0].Rows[0]["user_email"].ToString() != ""))
                {
                    feedback.user_email = set.Tables[0].Rows[0]["user_email"].ToString();
                }
                if ((set.Tables[0].Rows[0]["add_time"] != null) && (set.Tables[0].Rows[0]["add_time"].ToString() != ""))
                {
                    feedback.add_time = DateTime.Parse(set.Tables[0].Rows[0]["add_time"].ToString());
                }
                if ((set.Tables[0].Rows[0]["reply_content"] != null) && (set.Tables[0].Rows[0]["reply_content"].ToString() != ""))
                {
                    feedback.reply_content = set.Tables[0].Rows[0]["reply_content"].ToString();
                }
                if ((set.Tables[0].Rows[0]["reply_time"] != null) && (set.Tables[0].Rows[0]["reply_time"].ToString() != ""))
                {
                    feedback.reply_time = new DateTime?(DateTime.Parse(set.Tables[0].Rows[0]["reply_time"].ToString()));
                }
                if ((set.Tables[0].Rows[0]["is_lock"] != null) && (set.Tables[0].Rows[0]["is_lock"].ToString() != ""))
                {
                    feedback.is_lock = int.Parse(set.Tables[0].Rows[0]["is_lock"].ToString());
                }
                return feedback;
            }
            return null;
        }

        public bool Update(mfeedback model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update dt_feedback set ");
            builder.Append("title=@title,");
            builder.Append("content=@content,");
            builder.Append("user_name=@user_name,");
            builder.Append("user_tel=@user_tel,");
            builder.Append("user_qq=@user_qq,");
            builder.Append("user_email=@user_email,");
            builder.Append("add_time=@add_time,");
            builder.Append("reply_content=@reply_content,");
            builder.Append("reply_time=@reply_time,");
            builder.Append("is_lock=@is_lock");
            builder.Append(" where id=@id");
            OleDbParameter[] parameterArray = new OleDbParameter[] { new OleDbParameter("@title", OleDbType.VarChar, 100), new OleDbParameter("@content", OleDbType.VarChar), new OleDbParameter("@user_name", OleDbType.VarChar, 50), new OleDbParameter("@user_tel", OleDbType.VarChar, 30), new OleDbParameter("@user_qq", OleDbType.VarChar, 30), new OleDbParameter("@user_email", OleDbType.VarChar, 100), new OleDbParameter("@add_time", OleDbType.Date), new OleDbParameter("@reply_content", OleDbType.VarChar), new OleDbParameter("@reply_time", OleDbType.Date), new OleDbParameter("@is_lock", OleDbType.Integer, 4), new OleDbParameter("@id", OleDbType.Integer, 4) };
            parameterArray[0].Value = model.title;
            parameterArray[1].Value = model.content;
            parameterArray[2].Value = model.user_name;
            parameterArray[3].Value = model.user_tel;
            parameterArray[4].Value = model.user_qq;
            parameterArray[5].Value = model.user_email;
            parameterArray[6].Value = model.add_time;
            parameterArray[7].Value = model.reply_content;
            parameterArray[8].Value = model.reply_time;
            parameterArray[9].Value = model.is_lock;
            parameterArray[10].Value = model.id;
            return (DbHelperOleDb.ExecuteSql(builder.ToString(), parameterArray) > 0);
        }

        public void UpdateField(int id, string strValue)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update dt_feedback set " + strValue);
            builder.Append(" where id=" + id);
            DbHelperOleDb.ExecuteSql(builder.ToString());
        }
    }
}

